package cn.net.susan.seckill.business.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitMQ配置类
 *
 * @author 苏三，该项目是知识星球：java突击队 的内部项目
 * @date 2024/7/16 下午4:31
 */
@Slf4j
@Configuration
public class RabbitConfig {

    /**
     * 交换机
     */
    public static final String SECKILL_PRODUCT_EXCHANGE = "seckill_product_exchange";
    /**
     * 队列
     */
    public static final String SECKILL_PRODUCT_QUEUE = "seckill_product_queue";
    /**
     * 路由key
     */
    public static final String SECKILL_PRODUCT_ROUTING_KEY = "seckill_product_routing_key.#";

    /**
     * 交换机
     */
    public static final String CREATE_ORDER_EXCHANGE = "create_order_exchange";
    /**
     * 队列
     */
    public static final String CREATE_ORDER_QUEUE = "create_order_queue";
    /**
     * 路由key
     */
    public static final String CREATE_ORDER_ROUTING_KEY = "create_order_routing_key.#";

    /**
     * 延迟时间 （单位：ms(毫秒)）
     */
    public static final Integer DELAY_TIME = 10000;

    @Autowired
    private CachingConnectionFactory cachingConnectionFactory;

    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(cachingConnectionFactory);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        return rabbitTemplate;
    }


    /**
     * 创建商品秒杀交换机
     */
    @Bean("seckillProductExchange")
    public Exchange seckillProductExchange() {
        return new TopicExchange(SECKILL_PRODUCT_EXCHANGE, true, false);
    }


    /**
     * 创建商品秒杀队列
     */
    @Bean("seckillProductQueue")
    public Queue seckillProductQueue() {
        Map<String, Object> args = new HashMap<>(1);
        //过期时间，单位毫秒
        args.put("x-message-ttl", DELAY_TIME);
        return QueueBuilder.durable(SECKILL_PRODUCT_QUEUE).withArguments(args).build();
    }

    /**
     * 绑定商品秒杀交换机和队列
     */
    @Bean("seckillProductBinding")
    public Binding seckillProductBinding(@Qualifier("seckillProductQueue") Queue queue,
                                         @Qualifier("seckillProductExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(SECKILL_PRODUCT_ROUTING_KEY).noargs();
    }

    /**
     * 创建下单交换机
     */
    @Bean("createOrderExchange")
    public Exchange createOrderExchange() {
        return new TopicExchange(CREATE_ORDER_EXCHANGE, true, false);
    }


    /**
     * 创建下单队列
     */
    @Bean("createOrderQueue")
    public Queue createOrderQueue() {
        Map<String, Object> args = new HashMap<>(1);
        //过期时间，单位毫秒
        args.put("x-message-ttl", DELAY_TIME);
        return QueueBuilder.durable(CREATE_ORDER_QUEUE).withArguments(args).build();
    }

    /**
     * 绑定下单交换机和队列
     */
    @Bean("createOrderBinding")
    public Binding createOrderBinding(@Qualifier("createOrderQueue") Queue queue,
                                      @Qualifier("createOrderExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(CREATE_ORDER_ROUTING_KEY).noargs();
    }


    @Bean
    public MessageConverter jsonToMapMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
